package com.btks.jswater.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.btks.jswater.entity.WaterAllPoint;
import com.btks.jswater.response.ExceptionMessage;
import com.btks.jswater.response.Result;
import com.btks.jswater.service.impl.WaterMoniDataServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "移动APP接口")
@RestController
@RequestMapping("/updateInfo")
public class UploadMoniInfo {
    @Autowired
    private WaterMoniDataServiceImpl waterMoniDataService;
    @ApiOperation("上传数据、按照对应格式上传数据：需要字段{监测站种类，编号，水量，流量，水位}")
    @RequestMapping(value = "/updata", method = RequestMethod.POST)
    public void updateInfo(HttpServletResponse response,
                             @ApiParam(value = "测站编号") @RequestParam(value = "st_code", required = true) String st_code,
                             @ApiParam(value = "测站名") @RequestParam(value = "st_name", required = true) String st_name,
                             @ApiParam(value = "测站类型") @RequestParam(value = "st_type", required = false) String st_type,
                             @ApiParam(value = "时间") @RequestParam(value = "tm", required = true) String tm,
                             @ApiParam(value = "水量") @RequestParam(value = "st_q", required = false) BigDecimal st_q,
                             @ApiParam(value = "流量") @RequestParam(value = "st_w", required = false) BigDecimal st_w,
                             @ApiParam(value = "水位") @RequestParam(value = "st_l", required = false) BigDecimal st_l,
                             @ApiParam(value = "监测区编号") @RequestParam(value = "ad_code", required = false) String ad_code) throws ParseException {
        if (waterMoniDataService.isValidDate(tm)
                && waterMoniDataService.isNumeric(st_q.toString())
                && waterMoniDataService.isNumeric(st_w.toString())
                && waterMoniDataService.isNumeric(st_l.toString())) {
            int count = waterMoniDataService.dataIfExists(st_code,tm);
            if(count ==0){
                waterMoniDataService.insertMoniData(st_code, st_name, st_type, tm, st_w, st_q, st_l, ad_code);
                throw  new ExceptionMessage(200,"添加成功");
            }else{
                waterMoniDataService.updataByTmCode(st_code,tm,st_w,st_q,st_l);
                throw  new ExceptionMessage(200,"数据覆盖成功");
            }

        }else{
            throw  new ExceptionMessage(500,"请检查是否为数字，水位，流量，水量为数字");
        }
    }


    @ApiOperation("缓冲区查询，返回点列表")
    @GetMapping("/getBuffer")
    @ResponseBody
    Map<String, Object> getBuffer(
            @ApiParam(value = "经度-113.11111") @RequestParam(value = "x", required = true) float x,
            @ApiParam(value = "纬度") @RequestParam(value = "y", required = true) float y,
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "pageSize", required = true) int pageSize) {
        IPage<WaterAllPoint> waterAllPoints = waterMoniDataService.getBuffer(x, y, page, pageSize);
        waterAllPoints.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("data", waterAllPoints.getRecords());
        map.put("count", waterAllPoints.getTotal());
        return map;
    }

    @ApiOperation("按测站关键字搜索")
    @GetMapping("/getByKeyWords")
    @ResponseBody
    Map<String, Object> getByKeyWords(@ApiParam(value = "关键词", example = "小") @RequestParam(value = "keyWords", required = true) String keyWords,
                                      @ApiParam(value = "页码") @RequestParam(value = "page", required = true) int page,
                                      @ApiParam(value = "页面大小") @RequestParam(value = "pageSize", required = true) int pageSize) {
        IPage<WaterAllPoint> waterAllPoints = waterMoniDataService.getByName(keyWords, page, pageSize);
        waterAllPoints.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("data", waterAllPoints.getRecords());
        map.put("count", waterAllPoints.getTotal());
        return map;
    }


    @ApiOperation("按测站编号查询")
    @GetMapping("/getByStcd")
    @ResponseBody
    WaterAllPoint getByKeyWords(@ApiParam(value = "测站编号") @RequestParam(value = "stcd", required = true) String stcd) {
        WaterAllPoint waterAllPoints = waterMoniDataService.getByStcd(stcd);
        return waterAllPoints;
    }

}
